#include "MathTools.h"

CSavaCharData::CSavaCharData(char * fileName, char * mode)
{
	if (fileName == NULL || mode == NULL) {
		return;
	}
	m_pHandle = NULL;
	strcpy(m_cFileName, fileName);
	strcpy(m_cMode, mode);
	
	openFile(m_cFileName, m_cMode);
}

CSavaCharData::~CSavaCharData()
{
}

FILE * CSavaCharData::openFile(char * fileName, char * mode)
{
	closeFile();
	
	if (strlen(fileName) > 0 && strlen(mode) > 0) {
		m_pHandle = fopen(fileName, mode);
		return m_pHandle;
	}
	else {
		return NULL;
	}
}

bool CSavaCharData::closeFile()
{
	if (m_pHandle) {
		fclose(m_pHandle);
		m_pHandle = NULL;
	}
	return true;
}

int CSavaCharData::writeData(unsigned char * data, int nProtocolLen)
{
	if (m_pHandle == NULL) {
		return -1;
	}
	 
	int nRet = fwrite(data, sizeof(unsigned char), nProtocolLen, m_pHandle);
	return nRet;
}

int CSavaCharData::fileFlush()
{
	if (m_pHandle == NULL) {
		return -1;
	}
	fflush(m_pHandle);
	return 0;
}
